﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utility.NodeGraphs;
using Utility.Math;
using UnityEditor;
using Utility.Inspector;

namespace Utility.BehaviourTree
{
	[NodeEditor (typeof (VisualizerNode))]
	public class VisualizerNodeEditor : TreeNodeEditor
	{
		public override bool ShowAddChildPortButton (Node node) => false;
		protected override GenericMenu BuildNodeContextMenu (Node node) => null;
		protected override GenericMenu BuildPortContextMenu (NodePort port) => null;
		public override bool PortCanStartConnectionByLeftClick (Node node, int portIndex) => false;

		public override Vector2 GetContentSize (Node node)
		{
			float h_padding = 2;
			float h_line = EditorGUIUtility.singleLineHeight;

			float h = h_line * 3 + h_padding * 2;
			float w = 200;
			return new Vector2 (w, h);
		}

		protected override void DrawHeader (Rect headerRect, Node node)
		{
			var btNode = (node as VisualizerNode).btNode;

			Color c = Color.white;
			switch (btNode.Status)
			{
				case Status.Error:
				case Status.Failure:
					c = Color.red;
					break;
				case Status.Running:
					c = Color.yellow;
					break;
				case Status.Success:
					c = Color.green;
					break;
			}

			using (GUIColorScope.New (c))
			{
				base.DrawHeader (headerRect, node);
			}
		}

		protected override void DrawContents (Rect contentRect, Node node)
		{
			var btNode = (node as VisualizerNode).btNode;
			Rect rect = contentRect.ResizeHeight (EditorGUIUtility.singleLineHeight);

			using (EditorGUIWidthScope.NewLabelWidth (80))
			{				
				EditorGUI.TextField (rect, "Name", btNode.Name);
				rect = rect.ShiftUp ().ShiftUp (2);
				EditorGUI.TextField (rect, "Status", btNode.Status.ToString ());
				rect = rect.ShiftUp ().ShiftUp (2);
				EditorGUI.HelpBox (rect, btNode.Information, MessageType.Info);
			}
		}

		
	}
}